import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";

export default defineFakeRoute([
  // 文章列表
  {
    url: "/api/article/list",
    method: "get",
    response: ({ query }) => {
      const { page = 1, pageSize = 10 } = query;
      const title = query.title as string;
      const status = query.status as string;
      const category_id = query.category_id as string;
      const list = Array.from({ length: 20 }).map((_, index) => ({
        id: index + 1,
        title: faker.lorem.sentence(5),
        content: faker.lorem.paragraphs(3),
        author: faker.person.fullName(),
        publish_time: faker.date.past().toLocaleString(),
        category_id: faker.number.int({ min: 1, max: 3 }),
        category_name: `分类${faker.number.int({ min: 1, max: 3 })}`,
        status: faker.number.int({ min: 0, max: 2 }), // 0: 未审核, 1: 审核通过, 2: 审核未通过
        audit_reason: faker.lorem.sentence(),
        read_count: faker.number.int({ min: 100, max: 10000 }),
        comment_count: faker.number.int({ min: 0, max: 500 })
      }));

      let filteredList = list;
      if (title) {
        filteredList = filteredList.filter(item => item.title.includes(title));
      }
      if (status !== undefined && status !== null && status !== "") {
        filteredList = filteredList.filter(item => item.status === Number(status));
      }
      if (category_id) {
        filteredList = filteredList.filter(item => item.category_id === Number(category_id));
      }

      const total = filteredList.length;
      const paginatedList = filteredList.slice(
        (Number(page) - 1) * Number(pageSize),
        Number(page) * Number(pageSize)
      );

      return {
        success: true,
        code: 200,
        data: {
          list: paginatedList,
          total: total
        },
        message: "获取文章列表成功"
      };
    }
  },
  // 文章详情
  {
    url: "/api/article/detail",
    method: "post",
    response: ({ body }) => {
      const { id } = body;
      return {
        code: 200,
        data: {
          id: id,
          title: faker.lorem.sentence(5),
          content: faker.lorem.paragraphs(5),
          author: faker.person.fullName(),
          publish_time: faker.date.past().toLocaleString(),
          category_id: faker.number.int({ min: 1, max: 3 }),
          category_name: `分类${faker.number.int({ min: 1, max: 3 })}`,
          status: faker.number.int({ min: 0, max: 2 }), // 0: 未审核, 1: 审核通过, 2: 审核未通过
          audit_reason: faker.lorem.sentence(),
          read_count: faker.number.int({ min: 100, max: 10000 }),
          comment_count: faker.number.int({ min: 0, max: 500 })
        },
        message: "获取文章详情成功"
      };
    }
  },
  // 添加文章
  {
    url: "/api/article/add",
    method: "post",
    response: () => {
      return {
        code: 200,
        data: {
          id: faker.number.int()
        },
        message: "添加文章成功"
      };
    }
  },
  // 修改文章
  {
    url: "/api/article/update",
    method: "post",
    response: ({ body }) => {
      const { id, status, audit_reason } = body;
      console.log("update article", id, status, audit_reason);
      return {
        code: 200,
        message: "修改文章成功"
      };
    }
  },
  // 删除文章
  {
    url: "/api/article/del",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "删除文章成功"
      };
    }
  },
  // 审核文章
  {
    url: "/api/article/audit",
    method: "post",
    response: ({ body }) => {
      const { id, status, audit_reason } = body;
      console.log("audit article", id, status, audit_reason);
      return {
        code: 200,
        message: "审核文章成功"
      };
    }
  }
]);
